﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace IndexMarkSilverlightClient.Classes
{
    public enum Dock
    {
        Fill,
        Top,
        Bottom,
        Left,
        Right
    }

    public struct SizeColumn
    {
        public double ColumnWidth;
        public double DataGridWidth;
    }
    
    public class xDataGrid: DataGrid
    {
        public static readonly DependencyProperty WidthPercentageProperty = DependencyProperty.RegisterAttached(
                  "WidthPercentage",                                //Property name
                  typeof(SizeColumn),                                      //Property type
                  typeof(xDataGrid),                                //Type of the dependency property provider
                  new PropertyMetadata(WidthPercentageChanged));    //Callback invoked on property value change

        public static readonly DependencyProperty DockProperty = DependencyProperty.RegisterAttached(
                  "Dock",                                //Property name
                  typeof(Dock),                                      //Property type
                  typeof(xDataGrid),                                //Type of the dependency property provider
                  new PropertyMetadata(DockChanged));    //Callback invoked on property value change

        //public xDataGrid()
        //{
        //    this.SizeChanged += new SizeChangedEventHandler(xDataGrid_SizeChanged);
        //}

        //void xDataGrid_SizeChanged(object sender, SizeChangedEventArgs e)
        //{
        //    foreach (DataGridColumn collumn in (sender as xDataGrid).Columns)
        //    {
        //        SetWidthPercentage(collumn, e.NewSize.Width);
        //    }
        //}

           public static void SetWidthPercentage(DependencyObject obj, SizeColumn widthInPercentage)
           {
               DataGridColumn dgCol = obj as DataGridColumn;
               if (dgCol != null)
               {
                   double width = widthInPercentage.DataGridWidth;
                   dgCol.Width = new DataGridLength(width * widthInPercentage.ColumnWidth / 100);
               }
           }

           public void SetSizeColumn(DataGridColumn col, SizeColumn size)
           {
               SetWidthPercentage(col, size);
           }

           public static SizeColumn GetWidthPercentage(DependencyObject obj)
           {
               return (SizeColumn)obj.GetValue(WidthPercentageProperty);
           }

           public static void SetDock(DependencyObject obj, Dock dock)
           {
               xDataGrid dg = obj as xDataGrid;
               if (dg != null)
               {
                   if (dock == Dock.Fill)
                   {
                       dg.Width = (double)dg.Parent.GetValue(FrameworkElement.WidthProperty);
                       dg.Height = (double)dg.Parent.GetValue(FrameworkElement.HeightProperty);
                       //dg.Width = 800;
                   }
               }
           }

           public static Dock GetDock(DependencyObject obj)
           {
               return (Dock)obj.GetValue(DockProperty);
           }
   
           private static void WidthPercentageChanged(object sender, DependencyPropertyChangedEventArgs args)
           {
               
           }

           private static void DockChanged(object sender, DependencyPropertyChangedEventArgs args)
           {
               //Do some processing here when the attached property value has changed...
           }
    }
}
